package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.xinsmartsky.penmaji.NewGridView.NewGridView;
import com.xinsmartsky.penmaji.NewGridView.ReadHZK;
import com.xinsmartsky.penmaji.SerialPort.FileOperation;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

public class EditActivity extends Activity implements View.OnClickListener{

    private Button edit_btn_crtx = null;//插入图形
    private Button edit_btn_save = null;
    private TextView edit_edt_pinyin = null;
    private EditText edit_edt_shouxie = null;
    private TextView edit_edt_num = null;
    private Button edit_btn_js = null;
    private Button edit_btn_blank = null;
    private Button edit_btn_back = null;
    private LinearLayout edit_lin_gridview;
    private HorizontalScrollView edit_hs_gview;
    //当前年月日
    private Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    private Button edit_btn_date = null;
    private Button edit_btn_time = null;
    private String date;
    private String time;
    private String getpinyintext = null;
    private int devfd = -1;
    private int drawingnum = 0;//图标号
    private int chinesenum = 0;//字条号


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        devfd = getIntent().getIntExtra("editfd", 0);
        Log.i("fd", "editfd===" + devfd);
        FindViewById();
        setOnClickListener();
    }


    private void FindViewById() {
        edit_edt_pinyin = (TextView) findViewById(R.id.edit_edt_pinyin);
//        edit_edt_pinyin.setInputType(InputType.TYPE_NULL);//不弹出软键盘
        edit_edt_num = (TextView) findViewById(R.id.edit_edt_num);
        edit_edt_num.setInputType(InputType.TYPE_NULL);//不弹出软键盘
//        edit_edt_num.setInputType(InputType.TYPE_CLASS_NUMBER);//调用数字键盘
        edit_btn_save = (Button) findViewById(R.id.edit_btn_save);
        edit_btn_blank = (Button) findViewById(R.id.edit_btn_blank);
        edit_btn_back = (Button) findViewById(R.id.edit_btn_back);
        edit_btn_date = (Button) findViewById(R.id.edit_btn_date);
        edit_btn_time = (Button) findViewById(R.id.edit_btn_time);
        edit_btn_js = (Button) findViewById(R.id.edit_btn_js);
        edit_lin_gridview = (LinearLayout) findViewById(R.id.edit_lin_gridview);
        ((Button) findViewById(R.id.edit_btn_draw)).setOnClickListener(this);//绘图
        ((Button) findViewById(R.id.edit_btn_delete)).setOnClickListener(this);
        edit_hs_gview = (HorizontalScrollView) findViewById(R.id.edit_hs_gview);
//手写
//        edit_edt_shouxie.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                //在afterTextChanged中， 调用setText()方法会重新触发监听器， 并不断进行递归，最后程序崩溃，需合理退出递归
//                if (s.length() > 0) {
//                    String str = edit_edt_shouxie.getText().toString();
//                    ChineseToGraph(str, MatLastNum);
//                    edit_edt_shouxie.setText("");
//                }
//            }
//        });
    }

    private void setOnClickListener() {
        edit_edt_num.setOnClickListener(this);
        edit_edt_pinyin.setOnClickListener(this);//监听 跳到中文编辑子菜单
        edit_btn_blank.setOnClickListener(this);
        edit_lin_gridview.setOnClickListener(this);
        edit_btn_back.setOnClickListener(this);
        edit_btn_time.setOnClickListener(this);
        edit_btn_date.setOnClickListener(this);
        edit_btn_js.setOnClickListener(this);//计数
        edit_btn_save.setOnClickListener(this);
    }


    private int num;//图标（字条号）
    /*弹出选择图标（字条）对话框*/
    private void AlertDialog(String title, int array, final Class<?> cls){
        new AlertDialog.Builder(EditActivity.this)
                .setTitle(title)
                .setItems(array, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        num = which + 1;
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putInt("fd", devfd);
                        bundle.putInt("num", num);
                        intent.putExtras(bundle);
                        intent.setClass(EditActivity.this, cls);
                        startActivity(intent);
                    }
                }).show();
    }

    private String strtitle;
    private int resarray;
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            //数字、英文
            case R.id.edit_edt_num: {
                strtitle = "请选择字条";
                resarray = R.array.chinesenum;
                AlertDialog(strtitle, resarray, EnglishAndDigitActivity.class);
                break;
            }
            //汉字、混编
            case R.id.edit_edt_pinyin: {
                strtitle = "请选择字条";
                resarray = R.array.chinesenum;
                AlertDialog(strtitle, resarray, ChineseMixActivity.class);
                break;
            }
            //绘图
            case R.id.edit_btn_draw: {
                strtitle = "请选择图标";
                resarray = R.array.drawing;
                AlertDialog(strtitle, resarray, DrawActivity.class);
                break;
            }

            case R.id.edit_btn_blank: {
                break;
            }
            //
            case R.id.edit_btn_date: {
                //跳转界面
//                bundle.putInt("fd", devfd);
//                intent.putExtras(bundle);
//                intent.setClass(EditActivity.this, .class);
//                startActivity(intent);
//                c.setTimeInMillis(System.currentTimeMillis());
//                DatePickerDialog edit_dtpdlg = new DatePickerDialog(
//                        EditActivity.this, new DatePickerDialog.OnDateSetListener() {
//                    @Override
//                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                        date = (year + "/" + (monthOfYear + 1) + "/" + dayOfMonth);
//                    }
//                }, year, month, day);
//                edit_dtpdlg.show();
                break;
            }
            case R.id.edit_btn_time: {
//                bundle.putInt("fd", devfd);
//                intent.putExtras(bundle);
//                intent.setClass(EditActivity.this, DigitEglishActivity.class);
//                startActivity(intent);
                //设置时间
//                TimePickerDialog tmdlg = new TimePickerDialog(
//                        EditActivity.this, new TimePickerDialog.OnTimeSetListener() {
//                    @Override
//                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                        c.setTimeInMillis(System.currentTimeMillis());
//                        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
//                        c.set(Calendar.MINUTE, minute);
//                        c.set(Calendar.SECOND, 0);
//                        c.set(Calendar.MILLISECOND, 0);
//                        time = (hourOfDay + ":" + minute);
//                    }
//                }, hour, minute, true);
//                tmdlg.show();
                break;
            }
            //计数
            case R.id.edit_btn_js: {
//                bundle.putInt("fd", devfd);
//                intent.putExtras(bundle);
//                intent.setClass(EditActivity.this, DigitEglishActivity.class);
//                startActivity(intent);
                break;
            }
            case R.id.edit_btn_back: {
                this.finish();
                break;
            }
//喷印设置
            case R.id.edit_btn_save: {
                bundle.putInt("fd", devfd);
                intent.putExtras(bundle);
                intent.setClass(EditActivity.this, PrintSetActivity.class);
                startActivity(intent);
                break;
            }

        }

    }

    private void CreatDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(EditActivity.this);
        builder.setTitle("您正在插入中文信息  是否需要绘制图形？");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
    }

}
